﻿Module mdlSaveLoad
    Dim w As IO.StreamWriter
    Dim r As IO.StreamReader

    'Create Save Directory
    Public Sub createDirectory()
        Try
            My.Computer.FileSystem.CreateDirectory("\SaT Save Files")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    'Load Array
    Public Sub loadArray(ByVal arrayInfo As Array, ByVal className As String)
        If Dir$("C:\SaT Save Files", vbDirectory) = String.Empty Then
            MessageBox.Show("'C:\SaT Save Files' not detected, is the folder there?'", "Error", MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
        Else
            '   arrayAssignment = System.IO.File.ReadAllLines("C:\SaT Save Files\" & className & ".txt.", arrayInfo
        End If
    End Sub

    'Save Array
    Public Sub SaveArray(ByVal arrayInfo As Array, ByVal className As String)
        Try
            Dim blnContinue As Boolean = True
            If Dir$("C:\SaT Save Files", vbDirectory) = String.Empty Then
                Dim dr As DialogResult
                dr = MessageBox.Show("Save directory doesn't exist. Do you wish to create it now?", "Create save directory", _
                                MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If dr = DialogResult.Yes Then
                    createDirectory()
                    SaveArray(arrayInfo, className)
                Else
                    MessageBox.Show("Save Failed: Save folder could not be detected.", "Save Failed", MessageBoxButtons.OK, _
                                    MessageBoxIcon.Exclamation)
                    blnContinue = False

                End If
            End If
            If blnContinue = True Then
                Dim i As Integer
                System.IO.File.OpenWrite("C:\SaT Save Files\" & className & ".txt")
                While i < arrayInfo.Length
                End While
                MessageBox.Show("Save Successful" & vbNewLine & "Save Location: 'C:\SaT Save Files\" & className & "'", _
                                "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            End If
        Catch ex As Exception
            MessageBox.Show("Save failed:" & ex.Message, "Error", MessageBoxButtons.OK)
        End Try
    End Sub

    Sub SaveListbox(ByVal listbox As ListBox, ByVal className As ToolStripComboBox)
        Try
            Dim i As Integer
            w = New IO.StreamWriter(CStr(className.SelectedItem) & ".txt")
            For i = 0 To listbox.Items.Count - 1
                w.WriteLine(listbox.Items.Item(i))
            Next
            w.Close()
            MessageBox.Show("Save Successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        Catch ex As Exception
            MessageBox.Show("Save failed:" & ex.Message, "Error", MessageBoxButtons.OK)
        End Try

    End Sub

    'load Listbox
    Sub LoadListbox(ByVal listbox As ListBox, ByVal classname As ToolStripComboBox)
        Try
            r = New IO.StreamReader(CStr(classname.SelectedItem) & ".txt")
            While (r.Peek() > -1)
                listbox.Items.Add(r.ReadLine)
            End While
            r.Close()
            MessageBox.Show("Load Successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        Catch ex As Exception
            MessageBox.Show("Load failed:" & ex.Message, "Error", MessageBoxButtons.OK)

        End Try
    End Sub
End Module
